var express = require("express");
const User = require("../models/user_bean");
const userController = require("../controllers/user_controller");

var userRouter = express.Router();

/* GET users listing. */
userRouter.get("/", function (req, res, next) {
    res.send("用户首页");
});

// 注册    http://localhost/user/register      post
userRouter.post("/register", (req, res) => {
    console.log(req.body.username, req.body.password);

    userController.doRegister(
        new User(req.body.username, req.body.password),
        (ret) => res.json(ret),
        (err) => res.json(err)
    );
});

// 登录    http://localhost/user/login         post
userRouter.post("/login", (req, res) => {
    console.log(req.body.username, req.body.password);

    userController.doLogin(
        new User(req.body.username, req.body.password),
        (ret) => {
            // res.cookie("username", req.body.username);
            req.session.username = req.body.username;
            res.json(ret);
        },
        (err) => res.json(err)
    );
});

module.exports = userRouter;
